﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;

namespace ESpace.DalTools.Common
{
	public interface IGetDBTables
	{
		 DataTable GetDBTables(string connstr);
	}


	public class GetDBTablesFactory
	{
		public IGetDBTables Creator(string type)
		{
			switch (type)
			{
				case "SqlServer":
					return new SqlServerTables();

				case "MySql":
					return new MySqlTables();
				default:
					return new SqlServerTables();
			}
		}
	}


	public class SqlServerTables : IGetDBTables
	{


		public DataTable GetDBTables(string connstr)
		{


			SqlConnection _conn = new SqlConnection(connstr);
			_conn.Open();

			SqlCommand sqlComm = new SqlCommand("select * from sys.tables where name<>'sysdiagrams' and name<>'dtproperties'", _conn);
			sqlComm.CommandType = CommandType.Text;

			SqlDataAdapter sdaTables = new SqlDataAdapter(sqlComm);
			DataTable dtTables = new DataTable();
			sdaTables.Fill(dtTables);

			_conn.Close();
			return dtTables;
		}

	}

	public class MySqlTables : IGetDBTables
	{


		public DataTable GetDBTables(string connstr)
		{



			DataTable data = new DataTable();
			MySqlDataAdapter da = new MySqlDataAdapter();
			
			MySqlConnection mysqlconn = new MySqlConnection(connstr);
			mysqlconn.Open();

			da.SelectCommand = new MySqlCommand("show tables", mysqlconn);
			da.Fill(data);
			
			return data;

		}

	}
}
